<template>
    <div class="upImgOss">
        <el-upload
          class="upload-demo"
          drag
          action="https://jsonplaceholder.typicode.com/posts/"
          :http-request="uploadVideo"
          multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg文件，且不超过10mb</div>
        </el-upload>
    </div>
</template>

<script>
    import OSS from 'ali-oss'
    export default {
        name: "upImgOss",
        components: {},
        props: [],
        data() {
            return {
                uploadConf: {
                    region: null,
                    accessKeyId: null,
                    accessKeySecret: null,
                    bucket: null,
                    stsToken: null
                }
            }
        },
        provide: {},
        inject: [],
        computed: {},
        methods: {
            async uploadVideo(options){
                console.log(options.file.name);
                let fileName = options.file.name.lastIndexOf(".");//获取到文件名开始到最后一个“.”的长度。
                let fileNameLength = options.file.name.length;//获取到文件名长度
                let fileFormat = options.file.name.substring(fileName + 1, fileNameLength);//截取后缀名
                const client = new OSS({
                    region: 'oss-cn-chengdu',
                    accessKeyId: 'LTAI4G7xLwyqhsbHHEr77sDa',
                    accessKeySecret: 'tXuM1SF5aD04od2pFOXDbJupsZ1M2q',
                    bucket: 'yangshen520'
                });
                let name = Date.now()+'.' + fileFormat;
                try {
                    await client.put(name, options.file,(res)=>{
                        console.log(res);
                    });
                } catch (e) {
                    this.$message.error('上传失败')
                }
                let url ="https://yangshen520.oss-cn-chengdu.aliyuncs.com/" + name;
                console.log(url);
            }
        },
        mounted() {
        },
        watch: {},
    }
</script>

<style scoped lang='scss'>
    .upImgOss {

    }
</style>
